package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.QuestionDao;
import com.itheima.domain.store.Question;
import com.itheima.service.store.QuestionService;
import com.itheima.utils.MapperUtils;
import com.itheima.utils.UUIDUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.util.Date;
import java.util.List;

/**
 * 题库管理  -> 题目管理(业务层)
 */
public class QuestionServiceImpl implements QuestionService {

    @Override
    public PageInfo<Question> queryPage(Integer pageNum, Integer pageSize) throws Exception {
        SqlSession sqlSession = MapperUtils.getSqlSession();
        QuestionDao questionDao = sqlSession.getMapper(QuestionDao.class);
        PageHelper.startPage(pageNum,pageSize);
        List<Question> list = questionDao.findAll();
        PageInfo<Question> pageInfo = (PageInfo<Question>) new PageInfo<>(list);

        sqlSession.close();
        return pageInfo;
    }

    @Override
    public List<Question> findAll() throws Exception {

        SqlSession sqlSession = MapperUtils.getSqlSession();
        QuestionDao questionDao = sqlSession.getMapper(QuestionDao.class);

        List<Question> list = questionDao.findAll();

        sqlSession.close();

        return list;
    }

    @Override
    public Question findById(String id) throws Exception {
        SqlSession sqlSession = MapperUtils.getSqlSession();
        QuestionDao questionDao = sqlSession.getMapper(QuestionDao.class);

        Question question = questionDao.findById(id);

        sqlSession.close();

        return question;
    }

    @Override
    public void save(Question question) throws Exception {

        SqlSession sqlSession = MapperUtils.getSqlSession();
        QuestionDao questionDao = sqlSession.getMapper(QuestionDao.class);

        question.setId(UUIDUtil.getSimpleUUID());
        question.setCreateTime(new Date());//设置插入日期
        question.setReviewStatus("0");//设置审核状态为未审核


        questionDao.save(question);

        sqlSession.commit();
        sqlSession.close();


    }

    @Override
    public void deleteById(String... ids) throws Exception {

        SqlSession sqlSession = MapperUtils.getSqlSession();
        QuestionDao questionDao = sqlSession.getMapper(QuestionDao.class);

        for (String id : ids) {
            questionDao.deleteById(id);
        }

        sqlSession.commit();
        sqlSession.close();

    }

    @Override
    public void update(Question question) throws Exception {
        SqlSession sqlSession = MapperUtils.getSqlSession();
        QuestionDao questionDao = sqlSession.getMapper(QuestionDao.class);

        //TODO 设置上传的图片名字
        questionDao.update(question);

        sqlSession.commit();
        sqlSession.close();
    }
}

